<?php
namespace app\member\model;

use app\BaseModel;
use think\facade\Db;

class Log extends BaseModel
{
    //会员权益表
    protected $name = 'longbing_card_v2_syn_log';




    /**
     * @author chenniang
     * @DataTime: 2020-09-09 10:37
     * @功能说明:列表
     */
    public function storedList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-27 17:16
     * @功能说明:编辑
     */
    public function updateSome($id,$coupon,$uniacid){

        $coupon_model = new StoredCoupon();

        $coupon_model->where(['stored_id'=>$id])->delete();

        if(!empty($coupon)){

            foreach ($coupon as $value){

                $insert = [

                    'uniacid'  => $uniacid,

                    'coupon_id'=> $value['coupon_id'],

                    'stored_id'=> $id,

                    'num'      => $value['num']

                ];

                $coupon_model->insert($insert);

            }

        }

        return true;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function storedInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function storedUpdate($dis,$data){

        $data['update_time'] = time();

        if(isset($data['coupon'])){

            $coupon = $data['coupon'];

            unset($data['coupon']);
        }

        $res = $this->where($dis)->update($data);

        if(!empty($coupon)){

            $id = $dis['id'];

            $this->updateSome($id,$coupon,$data['uniacid']);
        }

        return $res;

    }










}